<?php

require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/modules/sfGuardRegister/lib/BasesfGuardRegisterActions.class.php');

/**
 * sfGuardRegister actions.
 *
 * @package    guard
 * @subpackage sfGuardRegister
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z jwage $
 */
class sfGuardRegisterActions extends BasesfGuardRegisterActions
{
    public function executeRegisterUser(sfWebRequest $request) {
        
        //$this->forward404Unless($request->isMethod(sfRequest::POST));
        
        if ($this->getUser()->isAuthenticated())
        {
          $this->getUser()->setFlash('notice', 'You are already registered and signed in!');
          $this->redirect('@sf_guard_register_group');
        }
    
        $this->form = new sfGuardRegisterForm();
        

        if ($request->isMethod('post'))
        {
          $this->form->bind($request->getParameter($this->form->getName()));

          if ($this->form->isValid())
          {
            $user = $this->form->save();
            $this->getUser()->signIn($user);
    
            $this->redirect('@sf_guard_register_group');
          }
        else {
          //echo "FAGFAG <BR>";
          //$this->setTemplate('registerUser');
        }
        }
        
        //this->unique_login = Doctrine_Core::getTable('sfGuardUser')->find(array($request->getParameter('username')));
    }

    public function executeRegisterGroup(sfWebRequest $request) {
     
     $query = $request->getParameter('query');
     $this->groups = "Enter a group's name";
     if ($query != '')
         {
             $this->groupsObj = Doctring_Core::getTable('GofishGroups')->findFromName($query);
             if ($this->groupsObj->size() > 0)
                 {
                     $this->groups = "";
                     
                     foreach ($this->groupsObj as $group) {
                        $this->groups = $this->groups . $group->getName();
                     }
                 }
         }
    }
    
    public function executeRegisterFindGroup(sfWebRequest $request) {
        $this->setTemplate('registerGroup');
        
        if (!$this->getUser()->isAuthenticated())
        {
          $this->redirect('@sf_guard_register');
        }
        
        $form = new GoSharkFindGroupForm();
        
        if ($request->isMethod('post'))
        {
            $form->bind($request->getParameter('findGroup_form'));
            
            if ($form->isValid())
            {

                //load group object
                $formGroup = Doctrine_Core::getTable('GofishGroups')->findOneByGroupName($form['group_name']->getValue());
                
                //retrieve user object
                $user = Doctrine_Core::getTable('GofishPlayer')->find($this->getUser()->getGuardUser()->getId());
                if (!$user->getGroupId())
                {
                    $user->setGroupId($formGroup->getId());
                    $user->save();
                }
                                    
                //add new relationship to table
                $relation = new GofishPlayerGroup();
                $relation->setGroupId($formGroup->getId());
                $relation->setPlayerId($this->getUser()->getGuardUser()->getId());

                try{
                    $relation->save();
                    $this->getUser()->setAttribute('current_group', $formGroup->getId());
                }
                catch (Exception $e)
                    {
                        echo "fail";
                        // error, probably already have relation in DB
                    }
                $form->save();
                $this->redirect('@homepage');
            }
            else
            {
                echo "fail";
            }
        }
    }
    
    public function executeRegisterCreateGroup(sfWebRequest $request) {
        $this->setTemplate('registerGroup');
        
        if (!$this->getUser()->isAuthenticated())
        {
          $this->redirect('@sf_guard_register');
        }
        
        $form = new GoSharkCreateGroupForm();
        
        if ($request->isMethod('post'))
        {
            $form->bind($request->getParameter('createGroup_form'));
            
            if ($form->isValid())
            {
                // create form
                $newGroup = new GofishGroups();
                if ($newGroup->checkGroupAvailability($form['group_name']->getValue()))
                {
                    $form->save();
                    
                    //retrieve group object
                    $formGroup = $form->getObject();
                    
                    //retrieve user object
                    $user = Doctrine_Core::getTable('GofishPlayer')->find($this->getUser()->getGuardUser()->getId());
                    if (!$user->getGroupId())
                    {
                        $user->setGroupId($formGroup->getId());
                        $user->save();
                    }


                    // new technique
                    $relation = new GofishPlayerGroup();
                    $relation->setGroupId($formGroup->getId());
                    $relation->setPlayerId($this->getUser()->getGuardUser()->getId());

                    
                    try{
                        $relation->save();
                        $this->getUser()->setAttribute('current_group', $formGroup->getId());
                    }
                    catch (Exception $e)
                    {
                        echo "fail";
                        // error, probably already have relation in DB
                    }
                    
                    $this->redirect('@homepage');
                }
                else
                {
                    echo "fail to create group, name already exists?";
                }
            }
            else
            {
                echo "fail to validify";
            }
        }
    }
}